package com.ks.app.ad.action;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.ks.app.ad.hibernate.KsAd;
import com.ks.app.ad.hibernate.KsAdImage;
import com.ks.app.ad.service.KsadService;
import com.ks.app.admin.hibernate.KsResolution;
import com.ks.app.admin.hibernate.KsUser;
import com.ks.app.admin.service.*;
import com.ks.app.admin.util.Pager;
import com.ks.app.admin.hibernate.KsProvinceInfo;
import com.ks.app.search.util.Constants;
import java.io.File;    
import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;


@ParentPackage("struts-default")
@Namespace("")
@InterceptorRefs( {
	@InterceptorRef("defaultStack"),
	@InterceptorRef(value = "fileUpload", params = {"allowedTypes","image/jpeg,image/gif,image/png","maximumSize", "97867700" })
})  
@Results( {
		@Result(name = "success", location = "/WEB-INF/admin/main.html"),
		@Result(name = "quit", location = "/WEB-INF/admin/send.jsp"),
		@Result(name = "error", location = "/WEB-INF/admin/error.jsp"),
		@Result(name = "AdInfo", location = "/WEB-INF/admin/Ad/AdInfo.jsp"),
		@Result(name = "addAd", location = "/WEB-INF/admin/Ad/addAd.jsp"),
		@Result(name = "IntiEditAd", location = "/WEB-INF/admin/Ad/EditAd.jsp"),
		@Result(name = "regAdInfo", location = "/WEB-INF/admin/Ad/regAdInfo.jsp"),
		@Result(name = "regaddAd", location = "/WEB-INF/admin/Ad/regaddAd.jsp"),
		@Result(name = "IntiEditregAd", location = "/WEB-INF/admin/Ad/EditregAd.jsp")
})
public class KsAdAction implements ServletRequestAware{
	private HttpServletRequest request;
	private String username;
	
	private File[] reso;
	private String[] resoFileName;
	
	@Resource
	private KsadService ksadService;
	@Resource
	private KsresolutionService ksresolutionService;
	@Resource
	private ProvinceService provinceService;
	
	
	private KsAd adinfo;
	private String pno;	
	private KsAdImage adimginfo;
	
	
	private File[] resoi;
	private String[] resoiFileName;
	
	public File[] getResoi() {
		return resoi;
	}
	public void setResoi(File[] resoi) {
		this.resoi = resoi;
	}
	public String[] getResoiFileName() {
		return resoiFileName;
	}
	public void setResoiFileName(String[] resoiFileName) {
		this.resoiFileName = resoiFileName;
	}
	public KsAdImage getAdimginfo() {
		return adimginfo;
	}
	public void setAdimginfo(KsAdImage adimginfo) {
		this.adimginfo = adimginfo;
	}
	public KsAd getAdinfo() {
		return adinfo;
	}
	public void setAdinfo(KsAd adinfo) {
		this.adinfo = adinfo;
	}
	public File[] getReso() {
		return reso;
	}
	public void setReso(File[] reso) {
		this.reso = reso;
	}
	public String[] getResoFileName() {
		return resoFileName;
	}
	public void setResoFileName(String[] resoFileName) {
		this.resoFileName = resoFileName;
	}
	
	public String getPno() {
		return pno;
	}
	public void setPno(String pno) {
		this.pno = pno;
	}	

	private HttpSession session;
	public HttpSession getSession() {
		return session;

	}
	public void setSession(HttpSession session) {
		this.session = session;
	}	
	public void setServletRequest(HttpServletRequest arg0) {
		request=arg0;
	}

	@SuppressWarnings("unused")
	private KsUser sessuser;
	public KsUser getSessuser() {
		return sessuser;
	}

	public void setSessuser(KsUser sessuser) {
		this.sessuser = sessuser;
	}
	
	
	@Action("/admin/Ad/AdInfo")
	public String AdInfo()//根据条件获得广告内容 分页展示
	{		
		 try {
			pno = request.getParameter("pno") == null ? "1" : request.getParameter("pno").toString().trim();
			String rid=request.getParameter("adid")== null ? "" : request.getParameter("adid").toString().trim();
			String adname=request.getParameter("adname")== null ? "" : request.getParameter("adname").toString().trim();
			String adurl=request.getParameter("adurl")== null ? "" : request.getParameter("adurl").toString().trim();
			String adstartdate=request.getParameter("adstartdate")== null ? "" : request.getParameter("adstartdate").toString().trim();
			String adenddate=request.getParameter("adenddate")== null ? "" : request.getParameter("adenddate").toString().trim();
			String adisshow=request.getParameter("adisshow")== null ? "" : request.getParameter("adisshow").toString().trim();
			String pos = request.getParameter("pos");
			if(pos !=null){
				if(adname.length()>0){
					adname = new String(adname.getBytes("ISO8859-1"),"utf-8");					
				}				
				if(adurl.length()>0){
					adurl = new String(adurl.getBytes("ISO8859-1"),"utf-8");					
				}
			}
			request.setAttribute("adname", adname);
			request.setAttribute("adurl", adurl);
			Pager pager = new Pager();
			int iPageNo = 1;
			try{
				iPageNo = Integer.parseInt(pno);
			}
			catch( Exception e ){        	
			}
			pager.setCntOfPage(20);
			pager.setCurPage(iPageNo);
			List<KsAd> querylist=ksadService.GetKsAdList(rid,adname,adurl, adstartdate, adenddate, adisshow, pager);
			request.setAttribute("list_AdInfo", querylist);
			request.setAttribute("PAGER", pager);
			return "AdInfo";
		}
		 catch (Exception e) 
		 {
			 e.printStackTrace();
			return Error();
		}
	}
	
	
	@Action("/admin/Ad/regAdInfo")
	public String regAdInfo()//根据条件获得广告内容 分页展示 对外客户
	{		
		 try {
			pno = request.getParameter("pno") == null ? "1" : request
						.getParameter("pno").toString().trim();
			username=request.getSession().getAttribute("userinfo").toString().trim();			
			Pager pager = new Pager();
			int iPageNo = 1;
			try{
				iPageNo = Integer.parseInt(pno);
			}
			catch( Exception e ){        	
			}
			pager.setCntOfPage(20);
			pager.setCurPage(iPageNo);
			List<KsAd> querylist=ksadService.GetKsregAdList(username, pager);
			request.setAttribute("list_AdInfo", querylist);
			request.setAttribute("PAGER", pager);
			return "regAdInfo";
		}
		 catch (Exception e) 
		 {
			return Error();
		}
	}
	
	
	@Action("/admin/Ad/addAd")
	public String addAd()//进入添加广告页面
    {  
		
		List<KsProvinceInfo> prolist = provinceService.selectProvince();		
		request.setAttribute("prolist", prolist);
		
		List<KsResolution> lst=	ksresolutionService.GetKsResolutionList("1");
		if(lst!=null&&lst.size()>0)
		{
			StringBuilder sbresl=new StringBuilder();			
			for(int i=0;i<lst.size();i++)
			{
				sbresl.append("<div class=\"l_f_body_row\"><div class=\"l_f_body_div1\">图片("+lst.get(i).getResolution()+"):</div>");
				sbresl.append("<div class=\"l_f_body_div5\"><input type=\"file\" style=\"width:450px\" id=\"resolution"+lst.get(i).getRsltid()+"\" name=\"reso\" />");						
				sbresl.append("</div></div>");
			}
			request.setAttribute("ResolutionList", sbresl.toString());
		}
		return "addAd";    	
    }	
	
	
	
	@Action("/admin/Ad/regaddAd")
	public String regaddAd()//进入添加广告页面对外客户
    {  
		List<KsProvinceInfo> prolist = provinceService.selectProvince();		
		request.setAttribute("prolist", prolist);
		List<KsResolution> lst=	ksresolutionService.GetKsResolutionList("1");
		if(lst!=null&&lst.size()>0)
		{
			StringBuilder sbresl=new StringBuilder();			
			for(int i=0;i<lst.size();i++)
			{
				sbresl.append("<div class=\"l_f_body_row\"><div class=\"l_f_body_div1\">图片("+lst.get(i).getResolution()+"):</div>");
				sbresl.append("<div class=\"l_f_body_div5\"><input type=\"file\" style=\"width:450px\" id=\"resolution"+lst.get(i).getRsltid()+"\" name=\"reso\" />");						
				sbresl.append("</div></div>");
			}
			request.setAttribute("ResolutionList", sbresl.toString());
		}
		return "regaddAd";    	
    }	
	
	
	@Action("/admin/Ad/Error")
	public String Error()//抛错跳转页面
	{			
		return "error";
	}	
	

	@SuppressWarnings("unchecked")
	@Action("/admin/Ad/EditAd")
	public String EditAd()//修改广告
    {   
		try
		{					
			List imgidlistnew=new ArrayList();	
			List<KsResolution> lst=	ksresolutionService.GetKsResolutionList("1");
			if(lst!=null&&lst.size()>0)
			{
				for(int j=0;j<lst.size();j++)
				{
					String paramname="r"+lst.get(j).getRsltid();
					String imgparam= request.getParameter(paramname) == null ? "" : request.getParameter(paramname).toString().trim();
					if(imgparam.length()>0)
					{
						imgidlistnew.add(lst.get(j).getResolution());
					}
				}
			}
			
			List imgidlist=new ArrayList();					
			List<KsAdImage> queryimglist=ksadService.GetKsAdimgByAdid(adinfo.getAdid());
			if(queryimglist!=null&&queryimglist.size()>0)
			{
				for(int j=0;j<queryimglist.size();j++)
				{
					String paramname="i"+queryimglist.get(j).getAdimgid();
					String imgparam= request.getParameter(paramname) == null ? "" : request.getParameter(paramname).toString().trim();
					if(imgparam.length()>0)
					{
						imgidlist.add(paramname.substring(1));
					}
				}
			}
			
			List<KsAdImage> lstimg=new ArrayList();
			List<KsAdImage> lstimgedit=new ArrayList();
			
			String path = Constants.CONS_PROPERTIES.getValue("filedir");
			
			if (reso!= null)//新增的分辨率 必须添加
			{
				if(imgidlistnew.size()==reso.length)
				{
					for (int i = 0; i < reso.length; i++) 
					{			
						
						File targeta = new File(path+"/Ad/"+adinfo.getAdid()+"/"+imgidlistnew.get(i).toString()+"/",resoFileName[i]);//	lst.get(i).getResolution()			
						FileUtils.copyFile(reso[i], targeta);	
						
						KsAdImage ki=new KsAdImage();
						ki.setImagepath(path+"/Ad/"+adinfo.getAdid()+"/"+imgidlistnew.get(i).toString()+"/"+resoFileName[i]);
						ki.setResolution(imgidlistnew.get(i).toString());
						ki.setAdid(adinfo.getAdid());
						lstimg.add(ki);
					}
				}
				else
				{
					return Error();
				}
			}
			if (resoi!= null)  //原有的图片 某个进行修改
			{	
				if(imgidlist.size()==resoi.length)
				{
					for (int i = 0; i < resoi.length; i++)
					{						
						File targeta = new File(path+"/Ad/"+adinfo.getAdid()+"/"+lst.get(i).getResolution()+"/",resoiFileName[i]);				
						FileUtils.copyFile(resoi[i], targeta);	
						
						List<KsAdImage> imglist=ksadService.GetKsAdimgByImgid(Long.parseLong(imgidlist.get(i).toString()));
						if(imglist!=null&&imglist.size()>0)
						{
							KsAdImage ki=imglist.get(0);
							ki.setImagepath(path+"/Ad/"+adinfo.getAdid()+"/"+lst.get(i).getResolution()+"/"+resoiFileName[i]);
							ki.setAdid(adinfo.getAdid());
							lstimgedit.add(ki);
						}
					}
				}
				else
				{
					return Error();
				}
			}		
			
			//---------获得修改后的省份字符�?形式是：|pid|pid|
			  String province= "|";
			  String[] str = request.getParameterValues("province");
			 if(null!=str &&str.length>0){
				 for(int i=0;i<str.length;i++){
						province +=str[i].toString()+"|";
					}
			 }			 
			adinfo.setProvince(province);
			username=request.getSession().getAttribute("userinfo").toString().trim();
			ksadService.updateKsAd(adinfo,lstimgedit,lstimg,username);
			return AdInfo();    
	    }
		 catch (Exception e) 
		 {
			e.printStackTrace();
			return Error();
		}
    }
	
	
	@SuppressWarnings("unchecked")
	@Action("/admin/Ad/EditregAd")
	public String EditregAd()//修改广告 对外客户
    {   
		try
		{					
			List imgidlistnew=new ArrayList();	
			List<KsResolution> lst=	ksresolutionService.GetKsResolutionList("1");
			if(lst!=null&&lst.size()>0)
			{
				for(int j=0;j<lst.size();j++)
				{
					String paramname="r"+lst.get(j).getRsltid();
					String imgparam= request.getParameter(paramname) == null ? "" : request.getParameter(paramname).toString().trim();
					if(imgparam.length()>0)
					{
						imgidlistnew.add(lst.get(j).getResolution());
					}
				}
			}
			
			List imgidlist=new ArrayList();					
			List<KsAdImage> queryimglist=ksadService.GetKsAdimgByAdid(adinfo.getAdid());
			if(queryimglist!=null&&queryimglist.size()>0)
			{
				for(int j=0;j<queryimglist.size();j++)
				{
					String paramname="i"+queryimglist.get(j).getAdimgid();
					String imgparam= request.getParameter(paramname) == null ? "" : request.getParameter(paramname).toString().trim();
					if(imgparam.length()>0)
					{
						imgidlist.add(paramname.substring(1));
					}
				}
			}
			
			List<KsAdImage> lstimg=new ArrayList();
			List<KsAdImage> lstimgedit=new ArrayList();
			
			String path = Constants.CONS_PROPERTIES.getValue("filedir");
			
			if (reso!= null)//新增的分辨率 必须添加
			{
				if(imgidlistnew.size()==reso.length)
				{
					for (int i = 0; i < reso.length; i++) 
					{			
						File targeta = new File(path+"/Ad/"+adinfo.getAdid()+"/"+imgidlistnew.get(i).toString()+"/",resoFileName[i]);//	lst.get(i).getResolution()			
						FileUtils.copyFile(reso[i], targeta);	
						
						KsAdImage ki=new KsAdImage();
						ki.setImagepath(path+"/Ad/"+adinfo.getAdid()+"/"+imgidlistnew.get(i).toString()+"/"+resoFileName[i]);
						ki.setResolution(imgidlistnew.get(i).toString());
						ki.setAdid(adinfo.getAdid());
						lstimg.add(ki);
					}
				}
				else
				{
					return Error();
				}
			}
			if (resoi!= null)  //原有的图片 某个进行修改
			{	
				if(imgidlist.size()==resoi.length)
				{
					for (int i = 0; i < resoi.length; i++)
					{						
						File targeta = new File(path+"/Ad/"+adinfo.getAdid()+"/"+lst.get(i).getResolution()+"/",resoiFileName[i]);				
						FileUtils.copyFile(resoi[i], targeta);	
						
						List<KsAdImage> imglist=ksadService.GetKsAdimgByImgid(Long.parseLong(imgidlist.get(i).toString()));
						if(imglist!=null&&imglist.size()>0)
						{
							KsAdImage ki=imglist.get(0);
							ki.setImagepath(path+"/Ad/"+adinfo.getAdid()+"/"+lst.get(i).getResolution()+"/"+resoiFileName[i]);
							ki.setAdid(adinfo.getAdid());
							lstimgedit.add(ki);
						}
					}
				}
				else
				{
					return Error();
				}
			}			
			
			//---------获得修改后的省份字符�?形式是：|pid|pid|
			  String province= "|";
			  String[] str = request.getParameterValues("province");
			 if(null!=str &&str.length>0){
				 for(int i=0;i<str.length;i++){
						province +=str[i].toString()+"|";
					}
			 }			 
			adinfo.setProvince(province);
			
			username=request.getSession().getAttribute("userinfo").toString().trim();
			ksadService.updateKsAd(adinfo,lstimgedit,lstimg,username);
			return regAdInfo();    
	    }
		 catch (Exception e) 
		 {
			e.printStackTrace();
			return Error();
		}
    }
	
	
	private String getinput(List<KsAdImage> queryimglist,KsResolution adrs)
	{
		if(queryimglist!=null&&queryimglist.size()>0)
		{
			StringBuilder sbresl=new StringBuilder();
			for(int j=0;j<queryimglist.size();j++)
			{
				if(adrs.getResolution().equals(queryimglist.get(j).getResolution()))
				{
					String imgpath=queryimglist.get(j).getImagepath().replace("\\", "/");
					sbresl.append("<div class=\"l_f_body_row\"><div class=\"l_f_body_div1\">图片("+queryimglist.get(j).getResolution()+"):</div>");
					sbresl.append("<div class=\"l_f_body_div5\"><input type=\"file\" style=\"width:450px\" id=\"resoi"+queryimglist.get(j).getAdimgid()+"\" name=\"resoi\" />");						
					sbresl.append("&nbsp;&nbsp;<a href=\"#\" onclick=\"clsGG('"+imgpath+"');\">查看原图</a>");
					sbresl.append("<input type=\"hidden\" style=\"width:10px\" id=\"i"+queryimglist.get(j).getAdimgid()+"\" name=\"i"+queryimglist.get(j).getAdimgid()+"\" />");
					sbresl.append("</div></div>");
					return sbresl.toString();
				}
			}
		}
		return "";
	}
	
	
	@Action("/admin/Ad/IntiEditAd")
	public String IntiEditAd()//初始化修改广告
	{			
		try {
			String id=request.getParameter("uid");
			List<KsAd> querylist=ksadService.GetKsAd(id);
			if(querylist.size()>0)
			{
				adinfo=querylist.get(0);
				List<KsProvinceInfo> prolist = provinceService.selectProvince();
					String province = adinfo.getProvince();
					ArrayList<Long> pList = new ArrayList<Long>();
					for(int i=0;i<prolist.size();i++){
						long proId = prolist.get(i).getPId();
						if(null!=province && province!=""){
							if(province.contains("|"+proId+"|")){
								pList.add(proId);
							}
						}
					}
					request.setAttribute("plist", pList);
					 request.setAttribute("prolist", prolist);
			
				List<KsResolution> lst=	ksresolutionService.GetKsResolutionList("1");
				if(lst!=null&&lst.size()>0)
				{
					StringBuilder sbresl=new StringBuilder();
					
					List<KsAdImage> queryimglist=ksadService.GetKsAdimgByAdid(adinfo.getAdid());
					if(queryimglist!=null&&queryimglist.size()>0)
					{
						for(int i=0;i<lst.size();i++)
						{
							String gin=getinput(queryimglist,lst.get(i));
							if(gin.length()>0)
							{
								sbresl.append(gin);
							}
							else
							{
								sbresl.append("<div class=\"l_f_body_row\"><div class=\"l_f_body_div1\">图片("+lst.get(i).getResolution()+"):</div>");
								sbresl.append("<div class=\"l_f_body_div5\"><input type=\"file\" style=\"width:450px\" id=\"resolution"+lst.get(i).getRsltid()+"\" name=\"reso\" />");						
								sbresl.append("<input type=\"hidden\" style=\"width:10px\" id=\"r"+lst.get(i).getRsltid()+"\" name=\"r"+lst.get(i).getRsltid()+"\" />");
								sbresl.append("</div></div>");
							}
						}
					}
					else
					{		
						for(int i=0;i<lst.size();i++)
						{
							sbresl.append("<div class=\"l_f_body_row\"><div class=\"l_f_body_div1\">图片("+lst.get(i).getResolution()+"):</div>");
							sbresl.append("<div class=\"l_f_body_div5\"><input type=\"file\" style=\"width:450px\" id=\"resolution"+lst.get(i).getRsltid()+"\" name=\"reso\" />");						
							sbresl.append("</div></div>");
						}
					}
					request.setAttribute("ResolutionList", sbresl.toString());
				}
			}
			return "IntiEditAd";
		} 
		catch (Exception e) {
			return Error();
		}
	}
	
	
	@Action("/admin/Ad/IntiEditregAd")
	public String IntiEditregAd()//初始化修改广告  对外客户
	{			
		try {
			String id=request.getParameter("uid");
			List<KsAd> querylist=ksadService.GetKsAd(id);
			if(querylist.size()>0)
			{
				adinfo=querylist.get(0);
				
				 List<KsProvinceInfo> prolist = provinceService.selectProvince();
					String province = adinfo.getProvince();
					ArrayList<Long> pList = new ArrayList<Long>();
					for(int i=0;i<prolist.size();i++){
						long proId = prolist.get(i).getPId();
						if(null!=province && province!=""){
							if(province.contains("|"+proId+"|")){
								pList.add(proId);
							}
						}
					}
					request.setAttribute("plist", pList);
					 request.setAttribute("prolist", prolist);
			
				List<KsResolution> lst=	ksresolutionService.GetKsResolutionList("1");
				if(lst!=null&&lst.size()>0)
				{
					StringBuilder sbresl=new StringBuilder();
					
					List<KsAdImage> queryimglist=ksadService.GetKsAdimgByAdid(adinfo.getAdid());
					if(queryimglist!=null&&queryimglist.size()>0)
					{
						for(int i=0;i<lst.size();i++)
						{
							String gin=getinput(queryimglist,lst.get(i));
							if(gin.length()>0)
							{
								sbresl.append(gin);
							}
							else
							{
								sbresl.append("<div class=\"l_f_body_row\"><div class=\"l_f_body_div1\">图片("+lst.get(i).getResolution()+"):</div>");
								sbresl.append("<div class=\"l_f_body_div5\"><input type=\"file\" style=\"width:450px\" id=\"resolution"+lst.get(i).getRsltid()+"\" name=\"reso\" />");						
								sbresl.append("<input type=\"hidden\" style=\"width:10px\" id=\"r"+lst.get(i).getRsltid()+"\" name=\"r"+lst.get(i).getRsltid()+"\" />");
								sbresl.append("</div></div>");
							}
						}
					}
					else
					{		
						for(int i=0;i<lst.size();i++)
						{
							sbresl.append("<div class=\"l_f_body_row\"><div class=\"l_f_body_div1\">图片("+lst.get(i).getResolution()+"):</div>");
							sbresl.append("<div class=\"l_f_body_div5\"><input type=\"file\" style=\"width:450px\" id=\"resolution"+lst.get(i).getRsltid()+"\" name=\"reso\" />");						
							sbresl.append("</div></div>");
						}
					}
					request.setAttribute("ResolutionList", sbresl.toString());
				}
			}
			return "IntiEditregAd";
		} 
		catch (Exception e) {
			return Error();
		}
	}

	@Action("/admin/Ad/SaveAd")
	public String SaveAd()//保存广告
    {   		
		try {			
			 String province = "|";
			 String[] str = request.getParameterValues("province");
			 if(null!=str &&str.length>0){
				 for(int i=0;i<str.length;i++){
						province +=str[i].toString()+"|";
					}
			 }
			 adinfo.setProvince(province);
			long adid=ksadService.addKsAd(adinfo);
			if(adid<=0)
			{
				return Error();
			}
			List<KsAdImage> lstimg=new ArrayList<KsAdImage>();
			List<KsResolution> lst=	ksresolutionService.GetKsResolutionList("1");
			
			String path = Constants.CONS_PROPERTIES.getValue("filedir");
			if (reso != null) {
				for (int i = 0; i < reso.length; i++) 
				{					
					File targeta = new File(path+"/Ad/"+adinfo.getAdid()+"/"+lst.get(i).getResolution()+"/",resoFileName[i]);				
					FileUtils.copyFile(reso[i], targeta);					
					KsAdImage ki=new KsAdImage();
					ki.setImagepath(path+"/Ad/"+adinfo.getAdid()+"/"+lst.get(i).getResolution()+"/"+resoFileName[i]);
					ki.setResolution(lst.get(i).getResolution());
					lstimg.add(ki);
				}
			}			
			username=request.getSession().getAttribute("userinfo").toString().trim();
			adinfo.setUsername(username);
			ksadService.addKsAd(adinfo,lstimg,username);
			return  AdInfo();
		}
		catch (Exception e) 
		{
			return Error();
		}   	
    }

	
	@Action("/admin/Ad/SaveregAd")
	public String SaveregAd()//保存广告 对外客户
    {   		
		try {	
			String province = "|";
			 String[] str = request.getParameterValues("province");
			 if(null!=str &&str.length>0){
				 for(int i=0;i<str.length;i++){
						province +=str[i].toString()+"|";
					}
			 }
			adinfo.setProvince(province);
			username=request.getSession().getAttribute("userinfo").toString().trim();			
			adinfo.setUsername(username);
			long adid=ksadService.addKsAd(adinfo);
			if(adid<=0)
			{
				return Error();
			}
			
			List<KsAdImage> lstimg=new ArrayList<KsAdImage>();
			List<KsResolution> lst=	ksresolutionService.GetKsResolutionList("1");
			
			String path = Constants.CONS_PROPERTIES.getValue("filedir");
			if (reso != null) {
				for (int i = 0; i < reso.length; i++) 
				{					
					File targeta = new File(path+"/Ad/"+adinfo.getAdid()+"/"+lst.get(i).getResolution()+"/",resoFileName[i]);				
					FileUtils.copyFile(reso[i], targeta);					
					KsAdImage ki=new KsAdImage();
					ki.setImagepath(path+"/Ad/"+adinfo.getAdid()+"/"+lst.get(i).getResolution()+"/"+resoFileName[i]);
					ki.setResolution(lst.get(i).getResolution());
					lstimg.add(ki);
				}
			}	
			ksadService.addKsAd(adinfo,lstimg,username);
			return  regAdInfo();
		}
		catch (Exception e) 
		{
			return Error();
		}   	
    }
	
	
	@Action("/admin/Ad/DeleteAd")
	public String DeleteAd()//删除广告
	{	 
		try {
			username=request.getSession().getAttribute("userinfo").toString().trim();
			String idlist=request.getParameter("uid");	
			int res=ksadService.deleteKsAd(idlist,username);
			if(res>0)
			{
			return AdInfo();
			}
			else
			{
				return Error();
			}
		}
		catch (Exception e) {
			return Error();
		}	
	}
	
	
	@Action("/admin/Ad/DeleteregAd")
	public String DeleteregAd()//删除广告 对外客户
	{	 
		try {
			username=request.getSession().getAttribute("userinfo").toString().trim();
			String idlist=request.getParameter("uid");	
			int res=ksadService.deleteKsregAd(idlist,username);			
			if(res>0)
			{
			return regAdInfo();
			}
			else
			{
				return Error();
			}
		}
		catch (Exception e) {
			return Error();
		}	
	}
	
}